package Class;

import reflection.Cat;

import java.lang.reflect.Field;

/**
 * @author 王艺锡
 * @version 1.0
 */
public class Class02 {
    public static void main(String[] args) throws Exception {
        String classPath = "Class.Car";
        //1.获得Car类对应的Class对象
        Class<?> cls = Class.forName(classPath);
        //2.输出cls
        System.out.println(cls);//显示cls对象是那个类的Class对象 Class.Car
        System.out.println(cls.getClass());//输出cls的运行类型 java.lang.Class
        //3.得到包名
        System.out.println(cls.getPackage().getName());
        //4.得到全类名
        System.out.println(cls.getName());
        //5.创建对象实例
        Car car = (Car) cls.newInstance();
        System.out.println(car);
        //6.得到对象属性(不能为私有)
        Field brand = cls.getField("brand");
        System.out.println(brand.get(car));
        //7.通过反射给属性赋值
        brand.set(car, "红旗h9");
        System.out.println(brand.get(car));
        //7.得到所有属性
        Field[] fields = cls.getFields();
        for (Field field :fields) {
            System.out.println(field.getName());
        }

    }
}
